package com.shaokel.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shaokel.commonutils.ReturnResult;
import com.shaokel.eduservice.entity.EduTeacher;
import com.shaokel.eduservice.entity.vo.TeacherQuery;
import com.shaokel.eduservice.service.EduTeacherService;
import com.shaokel.exception.shaokelException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 讲师 Controller
 *
 * @author 少丶客
 * @since 2020-05-25
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin   //解决跨域问题
@Api(description="讲师管理")
public class EduTeacherController {

    private Logger log = LoggerFactory.getLogger("EduTeacherController");

    @Autowired
    private EduTeacherService eduTeacherService;

    //查询所有的数据
    @GetMapping("/findAll")
    @ApiOperation(value = "所有讲师列表")
    public ReturnResult findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return ReturnResult.ok().data("items",list);
    }

    //逻辑删除
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据ID删除讲师")
    public ReturnResult removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id){
        boolean flag =  eduTeacherService.removeById(id);
        if(flag){
            return ReturnResult.ok();
        }else{
            return ReturnResult.error();
        }
    }

    //分页查询
    /**
     * @description:    分页查询
     * @author: 少丶客
     * @date:  16:23
     * @param: current  当前页
     * @param: limit    数据
     * @return: com.shaokel.commonutils.ReturnResult
     */
    @GetMapping("/query/{current}/{limit}")
    public ReturnResult queryTeacher(@PathVariable("current") long current,
                                     @PathVariable("limit") long limit){
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        eduTeacherService.page(pageTeacher,null);

        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//数据list集合

        return ReturnResult.ok().data("total",total).data("row",records);
    }

    //条件分页查询
    /**
     * @description:条件分页查询
     * @author: 少丶客
     * @date:  16:27
     * @param: current       当前页
     * @param: limit         分页数据
     * @param: teacherQuery  条件
     * @return: com.shaokel.commonutils.ReturnResult
     */
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public ReturnResult pageTeacherCondition(@ApiParam(name = "page", value = "当前页码", required = true)
                                        @PathVariable("page") Long page,
                                        @ApiParam(name = "limit", value = "每页记录数", required = true)
                                        @PathVariable("limit") Long limit,
                                        @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
                                        TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher> pageParams = new Page<>(page,limit);

        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        eduTeacherService.page(pageParams,wrapper);

        long total = pageParams.getTotal();//总记录数
        List<EduTeacher> records = pageParams.getRecords(); //数据list集合
        return ReturnResult.ok().data("total",total).data("rows",records);
    }

    //添加
    @PostMapping("/add")
    public ReturnResult addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return ReturnResult.ok();
        }else{
            return ReturnResult.error();
        }
    }

    //根据id查询
    @GetMapping("/get/{id}")
    public ReturnResult getTeacher(@PathVariable("id") String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
//        try { int i = 10/0; }catch (Exception e) { throw new shaokelException(404,"执行了自定义异常处理"); }
        if(!StringUtils.isEmpty(eduTeacher)){
            return ReturnResult.ok().data("teacher",eduTeacher);
        }else{
            return ReturnResult.error();
        }
    }

    //修改
    @PostMapping("/update")
    public ReturnResult updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if(flag){
            return ReturnResult.ok();
        }else{
            return ReturnResult.error();
        }
    }
}

